package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hazelcast.collection.impl.queue.QueueStoreWrapper;
import com.hazelcast.collection.impl.queue.QueueWaitNotifyKey;
import com.woniu.entity.*;
import com.woniu.mapper.*;
import com.woniu.service.IBuildingsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-03-29
 */
//楼号信息
@Service
public class BuildingsServiceImpl extends ServiceImpl<BuildingsMapper,Buildings> implements IBuildingsService {

    @Autowired
    private BuildingsMapper buildingsMapper;

    @Autowired
    private UnitsMapper unitMapper;

    @Autowired
    private FloorsMapper floorsMapper;

    @Autowired
    private HouseholdsMapper householdsMapper;

    @Autowired
    private HouseHoldinfosMapper houseHoldinfosMapper;

    /**
     删除楼号的时候同时删除单元表，楼层表，户型表，户型详细信息表
     */
    @Override
    @Transactional
    public int deleteById(String bid) {
        //先根据bid查出对应的 uid,fid,hid,hsid

        //uid
        QueryWrapper uquert =new QueryWrapper<Units>();
        uquert.in("Building_ID",Arrays.asList(bid));
        List<Units> ulist = unitMapper.selectList(uquert);
        //在将uid放入集合当中
        Set uid=new HashSet();
        for(int i = 0; i <ulist.size(); i++){
            uid.add(ulist.get(i).getUnitId());
        }
        //fid
        QueryWrapper fquery=new QueryWrapper();
        fquery.in("Unit_ID", uid);
        List<Floors> flist = floorsMapper.selectList(fquery);
        //将fid存入集合
        Set fid=new HashSet();
        for(int i = 0; i <flist.size(); i++){
            fid.add(flist.get(i).getFloorId());
        }

        //hid
        QueryWrapper hquery=new QueryWrapper();
        hquery.in("Floor_ID",fid);
        List<Households>hlist = householdsMapper.selectList(hquery);
        //将hid存入集合
        Set hid=new HashSet();
        for(int i = 0; i <hlist.size(); i++){
            hid.add(hlist.get(i).getHouseholdId());
        }

        //hsid
        QueryWrapper hsquery=new QueryWrapper();
        hsquery.in("HouseHold_ID",hid);
        List<HouseHoldinfos> hslist = houseHoldinfosMapper.selectList(hsquery);

        //将hsid存入集合
        Set hsid=new HashSet();
        for(int i = 0; i < hslist.size(); i++){
            hsid.add(hslist.get(i).getHouseholdinfoId());
        }



        //删除房屋详情信息
        int rows=0;
        if(hsid.size() > 0){
            rows= houseHoldinfosMapper.deleteBatchIds(hsid);
        }
        //删除房屋信息
        if(hid.size()>0){
            rows= householdsMapper.deleteBatchIds(hid);
        }

        //删除楼层信息
        if(fid.size()>0){
            rows=floorsMapper.deleteBatchIds(fid);
        }

        //删除单元
        if(uid.size()>0){
            rows=unitMapper.deleteBatchIds(uid);
        }

        //删除楼号
        if(bid!=null){
            rows=buildingsMapper.deleteBatchIds(Arrays.asList(bid));
        }


        return rows ;
    }
}
